<!-- 解决layer.open 不居中问题 -->
<!DOCTYPE html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="utf-8">
    <title>公司列表</title>
    <!--引入抽取css文件-->
    <%@include file="../common/public-css.jsp" %>

<body>

<div class="admin-main">

    <blockquote class="layui-elem-quote">
        <fieldset class="layui-elem-field">
            <legend>高级筛选</legend>
            <div class="layui-field-box layui-form">
                <form class="layui-form" action="" id="formData">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">公司名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="companyName" id="companyName" lay-verify=""
                                       placeholder="按公司名称搜索" autocomplete="off" class="layui-input"
                                       maxlength="20">
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <button class="layui-btn" id="search"><i class="layui-icon">&#xe615;</i> 搜索</button>
                            <button type="reset" class="layui-btn layui-btn-primary">清空</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>
    </blockquote>

    <fieldset class="layui-elem-field">
        <legend>公司列表&nbsp;<i class="fa fa-refresh" aria-hidden="true"></i></legend>
        </legend>
        <div class="layui-field-box layui-form">
            <blockquote class="layui-elem-quote">
                <button class="layui-btn layui-btn layui-btn-small layui-btn-normal hide checkBtn_13 addBtn"><i
                        class="layui-icon">
                    &#xe608;</i> 添加公司
                </button>
            </blockquote>
            <table id="dataTable" class="layui-table admin-table table-bordered display" cellspacing="0" width="100%">
                <thead>
                <tr>
                    <th>公司名称</th>
                    <th>所属公司</th>
                    <th>操作</th>
                </tr>
                </thead>
            </table>
        </div>
    </fieldset>
</div>

<!--引入抽取公共js-->
<%@include file="../common/public-js.jsp" %>
<script src="<%=request.getContextPath()%>/resources/js/common/jQuery.md5.js"></script>
<script>

    var form = null;
    var dataTable = null;
    var layer = null;
    $(document).ready(function () {
        dataTable = $('#dataTable').DataTable({
            "searching": false, "bStateSave": true, //状态保存，使用了翻页或者改变了每页显示数据数量，会保存在cookie中，下回访问时会显示上一次关闭页面时的内容。
            "processing": true,
            "serverSide": true,
            "bLengthChange": false, "bSort": false, //关闭排序功能
            //"pagingType": "bootstrap_full_number",
            'language': {
                'emptyTable': '没有数据',
                'loadingRecords': '加载中...',
                'processing': '查询中...',
                'search': '全局搜索:',
                'lengthMenu': '每页 _MENU_ 件',
                'zeroRecords': '没有您要搜索的内容',
                'paginate': {
                    'first': '第一页',
                    'last': '最后一页',
                    'next': '下一页',
                    'previous': '上一页'
                },
                'info': '第 _PAGE_ 页 / 总 _PAGES_ 页',
                'infoEmpty': '没有数据',
                'infoFiltered': '(过滤总件数 _MAX_ 条)'
            },
            //dataTable 加载加载完成回调函数
            "fnDrawCallback": function (sName, oData, sExpires, sPath) {
                checkJurisdiction(); //调用权限
                form.render();
            },
            "ajax": {
                url: AM.ip + "/company/list",
                "dataSrc": function (json) {
                    if (json.code == 200) {
                        return json.data;
                    }
                    return [];
                },
                "data": function (data) {
                    //高级查询参数
                    data.companyName = $("#companyName").val();
                }
            },
            "columns":[
                {"data": "companyName"},
                {"data": "parentName"},
            ],
            "columnDefs":[
                {
                    "render": function (data, type, row) {
                        if (data == null || data == "") {
                            return "--";
                        }
                        return data;
                    },
                    "targets": 1
                },
                {
                    "render": function (data, type, row) {
                        var btn = "";
                        if (row.isValid == 1) {
                            btn += "<button onclick='updateStatus(" + row.id + ",0)' class='layui-btn layui-btn-small layui-btn-normal hide checkBtn_11'><i class='fa fa-list fa-edit'></i>&nbsp;删除</button>";
                        }
                        btn += "<button onclick='updateData(" + row.id + ",\""+row.companyName+"\")' class='layui-btn layui-btn-small hide checkBtn_12'><i class='fa fa-list fa-edit'></i>&nbsp;查看/修改</button>";
                        return  btn;
                    },
                    "targets": 2
                },
            ]
        });

        $("#search").click(function () {
            dataTable.ajax.reload();
            return false;
        });

    });

    layui.use(['form', 'layedit', 'laydate'], function () {
        form = layui.form,
            layer = layui.layer;
    });


    //提供给子页面
    var closeNodeIframe = function () {
        dataTable.ajax.reload();
        var index = layer.load(1, {shade: [0.5, '#eee']});
        setTimeout(function () {
            layer.close(index);
        }, 600);
    }


    $(".addBtn").click(function () {
        // var html = '<div class="layui-form-item ">' +
        // '            <label class="layui-form-label"><span>公司名称</span><span class="font-red">*</span></label>' +
        // '            <div class="layui-input-inline">\n' +
        // '                <input type="text" id="companyName" name="companyName" lay-verify="required" placeholder="请输入分公司名称" autocomplete="off" class="layui-input" maxlength="100">' +
        // '            </div>' +
        //     '        </div>';
        // layer.open({
        //     content: html,
        //     area: ['500px', '200px'],
        //     yes: function(index, layero){
        //         var companyName = $("#companyName").val();
        //         if(null == companyName || companyName.length == 0){
        //             layer.msg("没有输入公司名称");
        //             return ;
        //         }
        //         AM.ajaxRequestData("post", false, AM.ip + "/company/save", {company:companyName,
        //         parentId : 1} , function(result) {
        //             if (result.flag == 0 && result.code == 200) {
        //                 //关闭iframe页面
        //                 layer.close(index);
        //             }
        //         });
        //     }
        // });
        layer.prompt({
            formType: 3,
            title: '请输入公司名称',
            area: ['500px', '200px'] //自定义文本域宽高
        }, function(value, index, elem){

            if(null == value || value.length == 0){
                layer.msg("没有输入公司名称"); return;}

            var arr = {
                companyName: value
            }
            AM.ajaxRequestData("post", false, AM.ip + "/company/save", arr, function (result) {
                if (result.flag == 0 && result.code == 200) {
                    layer.msg('操作成功.', {icon: 1});
                    layer.close(index);
                    dataTable.ajax.reload();
                    setTimeout(function () {
                        layer.closeAll();
                    }, 600);
                }
            });
        });


    })


    //查看/修改数据
    function updateData(id,companyName) {
        layer.prompt({
            formType: 3,
            title: '请输入公司名称',
            value:companyName,
            area: ['500px', '200px'] //自定义文本域宽高
        }, function(value, index, elem){
            if(null == value || value.length == 0){
                layer.msg("没有输入公司名称"); return;}

            var arr = {
                id : id,
                companyName: value
            }
            AM.ajaxRequestData("post", false, AM.ip + "/company/updateCompany", arr, function (result) {
                if (result.flag == 0 && result.code == 200) {
                    layer.msg('操作成功.', {icon: 1});
                    layer.close(index);
                    dataTable.ajax.reload();
                    setTimeout(function () {
                        layer.closeAll();
                    }, 600);
                }
            });
        });
    }


    //修改
    function updateStatus(id, status) {
        var statusMsg = "是否删除?";
        var arr = {
            id : id,
            isValid : status
        };
        layer.confirm(statusMsg, function(index){
            AM.ajaxRequestData("post", false, AM.ip + "/company/updateCompany", arr, function (result) {
                if (result.flag == 0 && result.code == 200) {
                    dataTable.ajax.reload();
                    layer.msg('操作成功.', {icon: 1});
                    var index = layer.load(1, {shade: [0.5, '#eee']});
                    setTimeout(function () {
                        layer.close(index);
                    }, 600);
                }
            });
            layer.close(index);
        });

    }









</script>
</body>
</head>
</html>
